cmake_minimum_required(VERSION 3.31)
project(Snake4c-Online C)

set(CMAKE_C_STANDARD 17)

# 设置专用的输出目录
set(OUTPUT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/output")
set(ASSETS_DIR "${OUTPUT_DIR}/assets")
set(LIB_DIR "${OUTPUT_DIR}/lib")
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS OFF)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${OUTPUT_DIR}")
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${OUTPUT_DIR}")
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${OUTPUT_DIR}")
#配置文件


# 创建输出目录和资源子目录
file(MAKE_DIRECTORY ${OUTPUT_DIR})
file(MAKE_DIRECTORY ${ASSETS_DIR})

# 添加可执行文件（WIN32 表示不弹出控制台窗口）
add_executable(Snake4c-Online WIN32 src/main.c
        src/snake.c
        src/snake.h
        src/snakeio.c
        src/snakeio.h
        src/util.c
        src/util.h
        src/game.c
        src/game.h
        lib/cjson/cJSON.c
)

# 指定头文件路径
target_include_directories(Snake4c-Online PRIVATE
        lib/sdl3/include
        lib/sdl3-img/include
        lib/sdl3-ttf/include
        lib/cjson
        lib/uthash
)


# 指定库目录
target_link_directories(Snake4c-Online PRIVATE
        lib/sdl3/lib
        lib/sdl3-img/lib
        lib/sdl3-ttf/lib
)

# 链接库
target_link_libraries(Snake4c-Online PRIVATE
        SDL3
        SDL3_image
        SDL3_ttf
        ws2_32
)


# 查找所有 DLL 文件
file(GLOB DLL_FILES
        ${CMAKE_SOURCE_DIR}/lib/sdl3/bin/*.dll
        ${CMAKE_SOURCE_DIR}/lib/sdl3-img/bin/*.dll
        ${CMAKE_SOURCE_DIR}/lib/sdl3-ttf/bin/*.dll
)

# 可选：添加资源文件（如图片、字体等）
file(GLOB RESOURCE_FILES
        ${CMAKE_SOURCE_DIR}/assets/*
)

add_custom_command(TARGET Snake4c-Online PRE_BUILD
        COMMAND ${CMAKE_COMMAND} -E make_directory ${ASSETS_DIR}
        COMMENT "Ensuring assets directory exists before copying"
)

# 复制 DLL 到输出目录
add_custom_command(TARGET Snake4c-Online POST_BUILD
        COMMAND ${CMAKE_COMMAND} -E copy_if_different
        ${DLL_FILES}
        ${OUTPUT_DIR}
        COMMENT "Copying DLLs to output directory"
)

# 复制资源文件到 output/assets 目录
add_custom_command(TARGET Snake4c-Online POST_BUILD
        COMMAND ${CMAKE_COMMAND} -E copy_if_different
        ${RESOURCE_FILES}
        ${ASSETS_DIR}
        COMMENT "Copying resources to output/assets directory"
)

# 拷贝 config.properties 到输出目录
add_custom_command(TARGET Snake4c-Online POST_BUILD
        COMMAND ${CMAKE_COMMAND} -E copy_if_different
        ${CMAKE_SOURCE_DIR}/config.properties
        ${OUTPUT_DIR}
        COMMENT "Copying config.properties to output directory"
)

